﻿// 功能：复选列表框控件
// 描述：
// 作者：温伟鹏
// 日期：2010/6/2 13:26:06

using System;
using System.Drawing;
using MeizuSDK.Drawing;
using System.Diagnostics;
using System.Collections.Generic;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 复选列表框控件。需要与ScrollableControl配合使用
    /// </summary>
    public class CheckBoxList : UnscrollableListBox
    {
        #region 私有变量
        private CheckBoxListItemCollection items;
        /// <summary>
        /// 被选中的项所显示的图片
        /// </summary>
        private ImagingHelper checkedItemImg;
        /// <summary>
        /// 未被选中项显示的图片
        /// </summary>
        private ImagingHelper uncheckedItemImg;
        /// <summary>
        /// 图像容器
        /// </summary>
        private ImageContainer imgContainer;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取或设置当前选中的列表项索引
        /// </summary>
        public override int SelectedIndex
        {
            get
            {
                return base.SelectedIndex;
            }
            set
            {
                if (value > 0 || value < Items.Count)
                {
                    if (Items[value] == null)
                        return;
                    Items[value].Selected = !Items[value].Selected;
                    InvalidateItem(value);
                    Update();
                }
            }
        }
        /// <summary>
        /// 获取列表控件的列表项集合
        /// </summary>
        public override ListItemCollection Items
        {
            get
            {
                if (items == null)
                {
                    items = new CheckBoxListItemCollection(this);
                }
                return items;
            }
        }
        /// <summary>
        /// 获取当前被选中的列表项数组
        /// </summary>
        public ListItem[] SelectedItems
        {
            get
            {
                return Items.SelectedItems;
            }
        }
        #endregion

        /// <summary>
        /// 构建复选列表框控件
        /// </summary>
        public CheckBoxList()
        {
            imgContainer = new ImageContainer();

            checkedItemImg = imgContainer.LoadImageFromMzResV2(MzResV2.Png_CheckBox_Selected, true);
            uncheckedItemImg = imgContainer.LoadImageFromMzResV2(MzResV2.Png_CheckBox_Unselected, true);
            // 自绘
            OwnerDrawItem = true;
        }

        #region 函数
        /// <summary>
        /// 引发 DrawItem 事件
        /// </summary>
        /// <param name="e">DrawItem事件参数</param>
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (!e.NeedRedrawItem())
                return;

            try
            {
                if (this.IsLButtonDown && !this.IsMouseDownAtScrolling && e.ItemIndex == (LastClickedIndex))
                {
                    e.DrawItemSelectedBackground();
                }

                ListItem item = Items[e.ItemIndex];

                if (item != null)
                {
                    Graphics g = e.Graphics;

                    int imgWidth = 0;
                    int imgHeight = 0;

                    // 获取图标所在矩形
                    Rectangle iconRect;

                    if (item.Selected)
                    {
                        iconRect = new Rectangle();
                        iconRect.X = e.ItemRectangle.X + 5;
                        iconRect.Y = e.ItemRectangle.Y + (e.ItemRectangle.Height - checkedItemImg.ImageHeight) / 2;
                        iconRect.Width = imgWidth = checkedItemImg.ImageWidth;
                        iconRect.Height = imgHeight = checkedItemImg.ImageHeight;

                        checkedItemImg.Draw(g, iconRect, true, true);
                    }
                    else
                    {
                        iconRect = new Rectangle();
                        iconRect.X = e.ItemRectangle.X + 5;
                        iconRect.Y = e.ItemRectangle.Y + (e.ItemRectangle.Height - uncheckedItemImg.ImageHeight) / 2;
                        iconRect.Width = imgWidth = uncheckedItemImg.ImageWidth;
                        iconRect.Height = imgHeight = uncheckedItemImg.ImageHeight;

                        uncheckedItemImg.Draw(g, iconRect, true, true);
                    }

                    using (SolidBrush brush = new SolidBrush(ForeColor))
                    {
                        using(StringFormat sf = new StringFormat())
                        {
                            sf.Alignment = StringAlignment.Near;// 文本靠左对齐
                            sf.LineAlignment = StringAlignment.Center;// 垂直居中

                            Rectangle textRect = e.ItemRectangle;// 获取文本内容所在矩形
                            textRect.Width -= (imgWidth + 5);
                            textRect.X += (imgWidth + 5);

                            g.DrawString(item.Text, Font, brush, textRect, sf);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 鼠标左键按下消息
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLButtonDown(MouseEventArgs e)
        {
            LastX = e.X;
            LastY = e.Y;

            int index = CalcItemIndexOfPos(e.X, e.Y);

            if (ContainsItem(index))
            {
                MouseDown = true;
                LastClickedIndex = index;

                InvalidateItem(index);
            }
            else
            {
                MouseDown = false;
                LastClickedIndex = -1;
                Invalidate();
            }
            Update();

            base.OnLButtonDown(e);
        }
        /// <summary>
        /// 鼠标左键抬起消息
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLButtonUp(MouseEventArgs e)
        {
            if (e.X != LastX || e.Y != LastY)
            {
                if (MouseDown)
                {
                    MouseDown = false;
                    LastClickedIndex = -1;
                    Invalidate();
                    Update();
                }
                return;
            }

            MouseDown = false;

            int index = CalcItemIndexOfPos(e.X, e.Y);

            if (ContainsItem(index))
            {
                LastClickedIndex = -1;
                SelectedIndex = index;
                base.OnLButtonUp(e);
                OnClick(new ListBoxClickEventArgs(index));
                return;
            }
            base.OnLButtonUp(e);
        }
        #endregion

        #region 销毁资源
        /// <summary>
        /// 销毁资源
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            checkedItemImg.Dispose();
            uncheckedItemImg.Dispose();
            imgContainer.Dispose();

            base.Dispose(disposing);
        }
        #endregion
    }
}
